package com.ych.sys.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ych.sys.entity.PageResult;
import com.ych.sys.entity.dto.UserDto;
import com.ych.sys.entity.dto.UserPageDto;
import com.ych.sys.entity.po.UserPo;
import com.ych.sys.mapper.UserMapper;
import com.ych.sys.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @BelongsProject: ape-frame
 * @BelongsPackage: com.ych.sys.service.impl
 * @Author: ych
 * @CreateTime: 2024-11-19  10:34
 * @Description: TODO
 * @Version: 1.0
 */

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public int addUser(UserDto userDto) {
        UserPo userPo = new UserPo();
        BeanUtils.copyProperties(userDto, userPo);
        return userMapper.insert(userPo);
    }

    @Override
    public int deleteUser(Integer id) {
        return userMapper.deleteById(id);
    }

    @Override
    public PageResult<UserPo> getUserPage(UserPageDto userPageDto) {
        UserPo userPo = new UserPo();
        BeanUtils.copyProperties(userPageDto, userPo);
        //首先创建一个Page对象，传入当前偏移量和页面大小
        Page<UserPo> userPoPage = new Page<>(userPageDto.getPageIndex(), userPageDto.getPageSize());
        IPage<UserPo> userPage = userMapper.getUserPage(userPo, userPoPage);
        PageResult<UserPo> userPoPageResult = new PageResult<>();
        userPoPageResult.loadData(userPage);
        return userPoPageResult;
    }

    @Override
    public PageResult<UserPo> getUserList(Integer pageNum, Integer pageSize) {
        Page<UserPo> page = new Page<>(pageNum, pageSize);
        IPage<UserPo> userPoIPage = userMapper.getUserList(page);
        PageResult<UserPo> userPoPageResult = new PageResult<>();
        userPoPageResult.loadData(userPoIPage);
        return userPoPageResult;
    }
}
